package org.component 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;
	import com.peachcastle.tools.Utils;
	import com.peachcastle.core.Display;
	
	import com.peachcastle.debug.Log;
	
	public class Button extends Display 
	{
		
		private var _label:TextField = null;
		
		private var _disabled:Boolean = false;
		
		public function Button(ui:MovieClip = null) 
		{
			super(ui);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			assest.stop();
			_label = assest["label"] || new TextField();
			_label.selectable = false;
			_label.mouseEnabled = false;
			assest.buttonMode = true;
			assest.mouseChildren = false;
		}
		
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
			
			assest.addEventListener(MouseEvent.ROLL_OVER, onAssestOver);
			assest.addEventListener(MouseEvent.ROLL_OUT, onAssestOut);
			assest.addEventListener(MouseEvent.CLICK, onAssestClick);
			assest.addEventListener(MouseEvent.MOUSE_DOWN, onAssestDown);
		}
		protected function onAssestDown(event:MouseEvent):void
		{
			if (disabled) return;
			assest.gotoAndStop("down");
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
		protected function onAssestClick(event:MouseEvent):void
		{
			if (disabled) return;
			//dispatchEvent(event);
		}
		protected function onAssestOver(event:MouseEvent):void
		{
			if (disabled) return;
			assest.gotoAndStop("over");
			dispatchEvent(event);
		}
		protected function onAssestOut(event:MouseEvent):void
		{
			if (disabled) return;
			assest.gotoAndStop("out");
			dispatchEvent(event);
		}
		
		public function setLabel(message:String = ""):void
		{
			_label.text = message;
		}
		
		public function getLabel():String
		{
			return _label.text;
		}
		
		public function hide():void
		{
			super.hideAssest();
		}
		public function show():void
		{
			super.showAssest();
		}
		
		public function get disabled():Boolean { return _disabled; }
		public function set disabled(value:Boolean):void
		{
			_disabled = value;
			if (value)
			{
				assest.buttonMode = false;
				assest.gotoAndStop("disabled");
			}else
			{
				assest.buttonMode = true;
				assest.gotoAndStop("out");
			}
		}
		
		public function setDisable(value:Boolean):void
		{
			_disabled = value;
			
			if (value)
			{
				assest.mouseEnabled = false;
				assest.mouseChildren = false;
				assest.filters = [new ColorMatrixFilter(Utils.getGrayMatrix())];
			} else
			{
				assest.mouseEnabled = true;
				assest.mouseChildren = false;
				
				assest.filters = null;
			}
		}
		
	}

}